<?php


namespace App\Utils;


use App\Constants\ErrorCode;
use App\Constants\RedisKey;
use App\Exception\BusinessException;
use App\Utils\AliSMS\Client;
use App\Utils\AliSMS\Request\SendSms;
use Hyperf\Redis\RedisFactory;

/**
 * 短信服务
 * Class SMS
 * @package App\Utils
 */
class SMS
{
    /**
     * @var \Hyperf\Redis\RedisProxy|mixed|\Redis|null
     */
    private $redis;

    public function __construct()
    {
        $this->redis = di(RedisFactory::class)->get('default');
    }

    /**
     * 验证码key
     * @param $phone
     * @return string
     */
    private function getCaptchaKey($phone)
    {
        return RedisKey::VERIFY_CODE . $phone;
    }

    /**
     * 发送验证码
     * @param $phone
     * @return int|bool
     */
    public function sendCaptcha($phone)
    {
        mt_srand();
        $code = mt_rand(1000, 9999);

        $client = new Client(
            [
                'accessKeyId' => config('alisms.accessKeyId'),
                'accessKeySecret' => config('alisms.accessKeySecret')
            ]
        );
        $sendSms = new SendSms();
        $sendSms->setPhoneNumbers($phone);
        $sendSms->setSignName(config('alisms.signName'));
        $sendSms->setTemplateCode(config('alisms.templateCode'));
        $sendSms->setTemplateParam(['code' => $code]);

        $smsData = $client->execute($sendSms);
        if ($smsData->Code == 'OK') {
            Log::get()->info(1, [$smsData]);
            $this->redis->setex($this->getCaptchaKey($phone), 5 * 60, $code);
            return true;
        } else {
            Log::get()->error(-1, [$smsData]);
            switch ($smsData->Code) {
                case 'isv.BUSINESS_LIMIT_CONTROL':
                    throw new BusinessException(ErrorCode::ALISMS_BUSINESS_LIMIT_CONTROL);
                    break;
                case 'isv.BLACK_KEY_CONTROL_LIMIT':
                    throw new BusinessException(ErrorCode::ALISMS_ISV_BLACK_KEY_CONTROL_LIMIT);
                    break;
                case 'isv.MOBILE_NUMBER_ILLEGAL':
                    throw new BusinessException(ErrorCode::ALISMS_ISV_MOBILE_NUMBER_ILLEGAL);
                    break;
                case 'Unsupport.CountryCode':
                    throw new BusinessException(ErrorCode::ALISMS_UNSUPPORT_COUNTRYCODE);
                    break;
                case 'Unknown.CountryCode':
                    throw new BusinessException(ErrorCode::ALISMS_UNKNOWN_COUNTRYCODE);
                    break;
                default:
                    throw new BusinessException(ErrorCode::ALISMS_ERROR);
            }
        }
    }

    /**
     * 校验验证码
     * @param $phone
     * @param $code
     * @return bool
     */
    public function checkCaptcha($phone, $code)
    {
        //测试使用
        if (\env('APP_ENV') != 'pro' && $code == '1234') {
            return true;
        }

        $key = $this->getCaptchaKey($phone);

        $sCode = $this->redis->get($key);

        if (!$code) {
            throw new BusinessException(ErrorCode::SMS_CAPTCHA_EMPTY);
        }

        if (!$sCode) {
            throw new BusinessException(ErrorCode::SMS_CAPTCHA_NOT_SEND);
        }

        if ($code != $sCode) {
            throw new BusinessException(ErrorCode::SMS_CAPTCHA_UNEQUAL);
        }

        $this->redis->del($key);
        return true;
    }


    public function sendMsg()
    {
        //TODO 发送指定模板信息
    }


}